﻿

//

using BootstrapBlazor.Components;
using LaserMark.Components.Events;
using LaserMark.Models;
using LaserMark.View.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LaserMark.Control.Service.mc;

namespace LaserMark.Extensions;
public static class CreateCardEx
{
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public static Task<bool> Create(Original original, MarkOption option, ManualResetEvent autoReset, double offsetx = 0, double offsety = 0, IRibbonTabEventBus bus = null, double degress = 0)
    {
        short sRtn;
        TCrdPrm crdPrm = new TCrdPrm();
        crdPrm.dimension = 2;
        crdPrm.synVelMax = 3000;
        crdPrm.synAccMax = 2;
        crdPrm.evenTime = 500;
        crdPrm.profile1 = 0;
        crdPrm.profile2 = 1;
        crdPrm.profile3 = 2;
        crdPrm.setOriginFlag = 1;
        crdPrm.originPos1 = 0;
        crdPrm.originPos2 = 0;
        crdPrm.originPos3 = 0;
        sRtn = GT_SetCrdPrm(0, 1, ref crdPrm);
        int space;
        sRtn = GT_CrdClear(0, 1, 0);
        var b = 1000 / (1 / option.XPluse.Value);
        foreach (var item in original.MarkPoints)
        {
            var temp = option.MarkTemplate.First(s => s.Order == item.Info);
            var (markOffsetx, markoffsety) = GetEdgeMargin(item.OffsetOrder, option);
            var (x, y) = CalcPointByPositionExtension.Calc(item, markOffsetx, markoffsety, temp.XMargin, temp.YMargin);
            var px = x / option.XPluse.Value;
            var py = y / option.YPluse.Value;
            var (x1, y1) = GetRotatePointExtension.RotatePointAroundCenter(px + offsetx, py + offsety, offsetx, offsety, degress);
            var xp = x1 + option.PhotoelectricitySetting.LightOffsetX / option.XPluse.Value;
            var yp = y1 + option.PhotoelectricitySetting.LightOffsetY / option.YPluse.Value;
            sRtn = GT_LnXY(0, 1, (int)xp, (int)yp, option.MarkVel.X / b, option.MarkAcc.X / 10000, 0, 0);
        }

        sRtn = GT_LnXY(0, 1, 0, 0, option.MarkVel.X / b, option.MarkAcc.X / 10000, 0, 0);
        sRtn = GT_CrdSpace(0, 1, out space, 0);
        sRtn = GT_CrdStart(0, 1, 0);
        return Task.FromResult(true);
    }

    private static (int x, int y) GetEdgeMargin(int order, MarkOption option) => order switch
    {
        1 => (option.MarkArgs.OffsetX1, option.MarkArgs.OffsetY1),
        2 => (option.MarkArgs.OffsetX2, option.MarkArgs.OffsetY2),
        3 => (option.MarkArgs.OffsetX3, option.MarkArgs.OffsetY3),
        4 => (option.MarkArgs.OffsetX4, option.MarkArgs.OffsetY4),
        5 => (option.MarkArgs.OffsetX5, option.MarkArgs.OffsetY5),
        _ => (0, 0)
    };
}
